//
//  DLCommonWebViewController.m
//  IBOSSSDK
//
//  Created by dgg_hzk on 2019/4/3.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "DLCommonWebViewController.h"
#import "DLJsBridgeBasePlugin.h"
#import <DGGWebBridge/DGGWebBridge.h>
#import <HBDNavigationBar/HBDNavigationBar.h>
#import <HBDNavigationBar/UIViewController+HBD.h>
#import <HBDNavigationBar/HBDNavigationController.h>

@interface DLCommonWebViewController ()

@property (nonatomic, assign) Class handleClass;

@property (nonatomic, strong) DLJsBridgeBasePlugin *bridgeHangle;

@end

@implementation DLCommonWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    kself
    self.jsBridgeRegisteredBlock = ^{
        [weakSelf initWebJsHandle];
    };
}

- (void)initWebJsHandle{
    if(self.handleClass){
        self.bridgeHangle = [[[self handleClass] alloc] init];
        [self.bridgeHangle injectBridgeWithWeb:self.webView];
        [self.bridgeHangle.bridge setWebViewDelegate:self];
        [self.bridgeHangle injectBaseHandleMethod];
        //self.bridgeHangle = [[self.handleClass alloc] initWithBridge:self.bridge];
    }else{
        //self.bridgeHangle =
        //[[DLJsBridgeBasePlugin alloc] initWithBridge:self.bridge];
        self.bridgeHangle = [DLJsBridgeBasePlugin injectBridgeWithWeb:self.webView];
    }
    [self.bridgeHangle.bridge setWebViewDelegate:self];
    //self.bridgeHangle.bridge = self.bridge;
    [self bindBridgeData];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-warning-option"
    kself
    [[RACObserve(self.webView, URL) takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
        if([weakSelf respondsToSelector:@selector(setLayoutNavigation)]){
            [weakSelf setLayoutNavigation];
        }
    }];
#pragma clang diagnostic pop
}

- (void)setLayoutNavigation{
    if([super respondsToSelector:@selector(dgg_layoutNavigation)]){
        [super dgg_layoutNavigation];
    }
}

- (void)registerHandleClass:(Class)handleClass{
    self.handleClass = handleClass;
    if(self.handleClass){
        self.bridgeHangle = [[[self handleClass] alloc] init];
        [self.bridgeHangle injectBridgeWithWeb:self.webView];
        [self.bridgeHangle.bridge setWebViewDelegate:self];
        [self.bridgeHangle injectBaseHandleMethod];
    }
//    if(self.bridgeHangle){
//        if(handleClass && ![NSStringFromClass([self.bridgeHangle class]) isEqualToString:NSStringFromClass(handleClass)]){
//            //self.bridgeHangle = [[self.handleClass alloc] initWithBridge:self.bridge];
//            //[self.handleClass injectBridgeWithWeb:self.webView];
//            [self bindBridgeData];
//        }
//    }
    [self bindBridgeData];
}

- (void)bindBridgeData{
    self.bridgeHangle.currentWebController = self;
    self.bridgeHangle.needRefreshController = self.rootVC;
}

- (void)dgg_setNav{
    //    // 导航栏颜色
    self.hbd_barTintColor = [UIColor colorWithHex:0x333333];
    // 导航栏两边按钮颜色
    self.hbd_tintColor = [UIColor whiteColor];
    //    // 导航栏上标题颜色
    NSDictionary *attributes = [UINavigationBar appearance].titleTextAttributes;
    NSMutableDictionary *mutableAttributes = [attributes mutableCopy];
    [mutableAttributes addEntriesFromDictionary:@{ NSForegroundColorAttributeName: CREATCOLORWITHSTRING(@"##FFFFFF") }];
    self.hbd_titleTextAttributes = mutableAttributes;
    //    // 状态栏是 default 还是 lightContent
    self.hbd_blackBarStyle = YES;
    self.hbd_barShadowHidden = YES;
}

// 观察者
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
